package com.lindroy.service

import com.lindroy.entity.Employee
import com.lindroy.repository.EmployeeRepo
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.data.domain.Example
import org.springframework.data.domain.Page
import org.springframework.data.domain.Pageable
import org.springframework.data.domain.Sort
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
import java.util.*

/**
 * @author Lin
 * @date 2019/10/10
 * @function
 * @Description
 */
@Service
@Transactional
class EmployeeService : EmployeeRepo {
    override fun findAll(sort: Sort): MutableList<Employee> = employeeRepo.findAll(sort)

    override fun <S : Employee?> findAll(example: Example<S>): MutableList<S> = employeeRepo.findAll(example)

    override fun <S : Employee?> findAll(example: Example<S>, sort: Sort) = employeeRepo.findAll(example,sort)
    /**
     * Returns a [Page] of entities meeting the paging restriction provided in the `Pageable` object.
     *
     * @param pageable
     * @return a page of entities
     */
    override fun findAll(pageable: Pageable)=employeeRepo.findAll(pageable)

    /**
     * Returns a [Page] of entities matching the given [Example]. In case no match could be found, an empty
     * [Page] is returned.
     *
     * @param example must not be null.
     * @param pageable can be null.
     * @return a [Page] of entities matching the given [Example].
     */
    override fun <S : Employee?> findAll(example: Example<S>, pageable: Pageable) = employeeRepo.findAll(example,pageable)

    /**
     * Returns the number of instances matching the given [Example].
     *
     * @param example the [Example] to count instances for. Must not be null.
     * @return the number of instances matching the [Example].
     */
    override fun <S : Employee?> count(example: Example<S>)=employeeRepo.count(example)

    /**
     * Deletes the given entities in a batch which means it will create a single [Query]. Assume that we will clear
     * the [javax.persistence.EntityManager] after the call.
     *
     * @param entities
     */
    override fun deleteInBatch(entities: MutableIterable<Employee>) {
    }

    /**
     * Deletes all entities in a batch call.
     */
    override fun deleteAllInBatch() {
    }

    /**
     * Saves an entity and flushes changes instantly.
     *
     * @param entity
     * @return the saved entity
     */
    override fun <S : Employee?> saveAndFlush(entity: S): S=employeeRepo.saveAndFlush(entity)
    /**
     * Flushes all pending changes to the database.
     */
    override fun flush() =employeeRepo.flush()

    /**
     * Returns a single entity matching the given [Example] or null if none was found.
     *
     * @param example must not be null.
     * @return a single entity matching the given [Example] or [Optional.empty] if none was found.
     * @throws org.springframework.dao.IncorrectResultSizeDataAccessException if the Example yields more than one result.
     */
    override fun <S : Employee?> findOne(example: Example<S>): Optional<S> = employeeRepo.findOne(example)

    /**
     * Returns a reference to the entity with the given identifier. Depending on how the JPA persistence provider is
     * implemented this is very likely to always return an instance and throw an
     * [javax.persistence.EntityNotFoundException] on first access. Some of them will reject invalid identifiers
     * immediately.
     *
     * @param id must not be null.
     * @return a reference to the entity with the given identifier.
     * @see EntityManager.getReference
     */
    override fun getOne(id: Int): Employee =employeeRepo.getOne(id)

    /**
     * Checks whether the data store contains elements that match the given [Example].
     *
     * @param example the [Example] to use for the existence check. Must not be null.
     * @return true if the data store contains elements that match the given [Example].
     */
    override fun <S : Employee?> exists(example: Example<S>)=employeeRepo.exists(example)


    @Autowired
    lateinit var employeeRepo: EmployeeRepo

   /* override fun findByPage(pageNo: Int, pageSize: Int): List<Employee>? {
        val pageRequest=PageRequest.of(pageNo,pageSize)
        return employeeRepo.findAll()
    }*/
    override fun findByFirstName(firstName: String)=employeeRepo.findByFirstName(firstName)

    override fun findByLastName(lastName: String) = employeeRepo.findByLastName(lastName)

   /* override fun insert(employee: Employee){
        employeeRepo.save(employee)
    }*/


    /**
     * Saves a given entity. Use the returned instance for further operations as the save operation might have changed the
     * entity instance completely.
     *
     * @param entity must not be null.
     * @return the saved entity will never be null.
     */
    override fun <S : Employee?> save(entity: S): S? = employeeRepo.save(entity)

    /**
     * Returns all instances of the type.
     *
     * @return all entities
     */
    override fun findAll():MutableList<Employee> = employeeRepo.findAll()

    /**
     * Deletes the entity with the given id.
     *
     * @param id must not be null.
     * @throws IllegalArgumentException in case the given `id` is null
     */
    override fun deleteById(id: Int) =employeeRepo.deleteById(id)

    /**
     * Deletes the given entities.
     *
     * @param entities
     * @throws IllegalArgumentException in case the given [Iterable] is null.
     */
    override fun deleteAll(entities: MutableIterable<Employee>) =employeeRepo.deleteAll(entities)

    /**
     * Deletes all entities managed by the repository.
     */
    override fun deleteAll()=employeeRepo.deleteAll()

    /**
     * Saves all given entities.
     *
     * @param entities must not be null.
     * @return the saved entities will never be null.
     * @throws IllegalArgumentException in case the given entity is null.
     */
    override fun <S : Employee?> saveAll(entities: MutableIterable<S>): MutableList<S>? =
        employeeRepo.saveAll(entities)

    /**
     * Returns the number of entities available.
     *
     * @return the number of entities
     */
    override fun count(): Long = employeeRepo.count()

    /**
     * Returns all instances of the type with the given IDs.
     *
     * @param ids
     * @return
     */
    override fun findAllById(ids: MutableIterable<Int>): MutableList<Employee>? = employeeRepo.findAllById(ids)

    /**
     * Returns whether an entity with the given id exists.
     *
     * @param id must not be null.
     * @return true if an entity with the given id exists, false otherwise.
     * @throws IllegalArgumentException if `id` is null.
     */
    override fun existsById(id: Int): Boolean = employeeRepo.existsById(id)

    /**
     * Retrieves an entity by its id.
     *
     * @param id must not be null.
     * @return the entity with the given id or Optional#empty() if none found
     * @throws IllegalArgumentException if `id` is null.
     */
    override fun findById(id: Int): Optional<Employee>? = employeeRepo.findById(id)

    /**
     * Deletes a given entity.
     *
     * @param entity
     * @throws IllegalArgumentException in case the given entity is null.
     */
    override fun delete(entity: Employee) {
    }

}